package com.fare.taluo.util.excel.engine.module;


import java.util.Map;

import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WriteException;

import org.apache.commons.lang.StringUtils;


public class ExcelCellFormat {

	/**
	 * 设置单元格格式
	 * @param map
	 * @return
	 */
	public  WritableCellFormat getCellFormat(Map<String, String> map) {
		WritableCellFormat cellFormat = new WritableCellFormat();
		getCellFormat(map,cellFormat);
		return cellFormat;
	}
	
	/**
	 * 设置单元格格式
	 * @param map
	 * @param cellFormat
	 * @return
	 */
	public  WritableCellFormat getCellFormat(Map<String, String> map,WritableCellFormat cellFormat) {
		if(cellFormat==null){
			cellFormat=  new WritableCellFormat();
		}
		try {
			setCellAlignment(map, cellFormat);
			setCellVerticalAlignment(map,cellFormat);
			setCellBorder(map,cellFormat);
			setWrap(map,cellFormat); 
			setCellFont(map,cellFormat);
			setCellBackground(map,cellFormat);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cellFormat;
	}
	
	/**
	 * 设置单元格的背景色
	 * @param map
	 * @param cellFormat
	 * @throws WriteException
	 */
	private void setCellBackground(Map<String, String> map,WritableCellFormat cellFormat) throws WriteException{
		String temp = map.get("backgroundcolor");
		if(temp!=null){
			int i = Integer.parseInt(temp);
			cellFormat.setBackground(Colour.getInternalColour(i));
		}
	}
	
	
	
	
	/**
	 * 设置单元格字体
	 * @param map
	 * @param cellFormat
	 * @throws WriteException 
	 * @throws NumberFormatException 
	 */
	private void setCellFont(Map<String, String> map,WritableCellFormat cellFormat) throws Exception{
		WritableFont wf = new WritableFont(WritableFont.TIMES);	
		String font = map.get("font");
		String bold = map.get("bold");
		String size = map.get("size");
		String color = map.get("color");
		String underlineStyle = map.get("underlinestyle");		
		//设置字体
		if(StringUtils.isNotEmpty(font)){
			 wf = new WritableFont(WritableFont.createFont(font));	
		}

		//设置字体大小
		if(StringUtils.isNumeric(size)){			
			wf.setPointSize(Integer.parseInt(size));
		}
		//是否需要加粗
		if("true".equalsIgnoreCase(bold)){
			wf.setBoldStyle(WritableFont.BOLD);
		}
		//设置字体颜色
		if(color!=null){
			wf.setColour(Colour.getInternalColour(Integer.parseInt(color)));
		}
		//设置下划线类型
		if(StringUtils.isNumeric(underlineStyle)){
			wf.setUnderlineStyle(UnderlineStyle.getStyle(Integer.parseInt(underlineStyle)));
		}		
		cellFormat.setFont(wf);
		
	}
	
	/**
	 * 是否自动换行
	 * @param wrap
	 * @param cellFormat
	 * @throws Exception
	 */
	private void setWrap(Map<String, String> map,WritableCellFormat cellFormat) throws Exception{
		String wrap = map.get("wrap");
		if("true".equalsIgnoreCase(wrap)){
			cellFormat.setWrap(true);
		}else{
			cellFormat.setWrap(false);
		}
	}
	
	/**
	 * 设置边框及边框线条的类型
	 * @param map
	 * @param cellFormat
	 * @throws WriteException
	 */
	private void setCellBorder(Map<String, String> map ,WritableCellFormat cellFormat) throws WriteException{
		//
		String border = map.get("border");
		String borderLineStyle= map.get("borderlinestyle");
		BorderLineStyle bls=null;
		Border b=null;
		//设置边框的线条类型
		if(StringUtils.isNotEmpty(borderLineStyle)){
			bls = (BorderLineStyle) BorderLineStyle.getStyle(Integer.parseInt(borderLineStyle));
		}

		//设置边框的类型
		if("all".equalsIgnoreCase(border)){
			b=Border.ALL;
		}else if("top".equalsIgnoreCase(border)){
			b=Border.TOP;
		}else if("bottom".equalsIgnoreCase(border)){
			b=Border.BOTTOM;
		}else if("left".equalsIgnoreCase(border)){
			b = Border.LEFT;
		}else if("right".equalsIgnoreCase(border)){
			b = Border.RIGHT;
		}else{
			b=Border.NONE;
		}
		//设置边框的颜色
		if(map.get("bordercolor")!=null){
			cellFormat.setBorder(b, bls,Colour.getInternalColour(Integer.parseInt(map.get("bordercolor"))));
		}else{
			cellFormat.setBorder(b, bls);
		}				
		
	}

	/**
	 * 设置单元格的垂直对齐方式
	 * @param map
	 * @param cellFormat
	 * @throws WriteException
	 */
	private void setCellVerticalAlignment(Map<String, String> map , WritableCellFormat cellFormat)
			throws WriteException {
		String str=map.get("verticalalignment");
		if ("top".equalsIgnoreCase(str)) { //靠上
			cellFormat.setVerticalAlignment(VerticalAlignment.TOP);
		} else if ("bottom".equalsIgnoreCase(str)) {
			cellFormat.setVerticalAlignment(VerticalAlignment.BOTTOM);//靠下
		}else{
			cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//居中
		} 

	}
	
	/**
	 * 设置单元格的水平对齐方式
	 * @param map
	 * @param cellFormat
	 * @throws WriteException
	 */
	private void setCellAlignment(Map<String, String> map , WritableCellFormat cellFormat)
			throws WriteException {
		String str = map.get("alignment");
		if ("left".equalsIgnoreCase(str)) { //左对齐
			cellFormat.setAlignment(Alignment.LEFT);
		} else if ("centre".equalsIgnoreCase(str)||"center".equalsIgnoreCase(str)) { //居中对齐
			cellFormat.setAlignment(Alignment.CENTRE);
		} else if ("right".equalsIgnoreCase(str)) { //右对齐
			cellFormat.setAlignment(Alignment.RIGHT);
		} else if ("fill".equalsIgnoreCase(str)) {  //填充
			cellFormat.setAlignment(Alignment.FILL);
		}else if ("justify".equalsIgnoreCase(str)) { //两端对齐
			cellFormat.setAlignment(Alignment.JUSTIFY);
		}else {
			cellFormat.setAlignment(Alignment.GENERAL);//常规
		}

	}

}
